<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
class Master extends Controller
{
    public function index(){
       	$master=Db::name("master")->select();
       	$this->assign("master",$master);
       	return $this->fetch();
       }
       public function delete($id){
       	//先把对应的大师头像删除
          $master=Db::name("master")->find($id);
          $pic=$master["masterpic"];
          $file=ROOT_PATH."public".DS."static".DS."images".DS.$pic;
          if(file_exists($file))unlink($file);

       	if(Db::name("master")->delete($id)) 
       	{
       	   $this->success('删除成功');
       	}else $this->fail('删除失败');
       }

public function add(){
    return $this->fetch();
}

public function masteradd(){
       $file = request()->file('pic'); 
        //移动到框架应用根目录/public/static/images/ 目录下 
       var_dump($file);
        if($file){
          $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'imagemaster'); 
          if ($info) { 
              $filename=$info->getFilename(); 
          } else { 
              $this->error($file->getError()); 
          } 
        }

    if(request()->isPost()){
     
      $data=[
        'clubid'=>input('param.clubid'),
        'masterpic'=>$filename,
        'mastername'=>input('param.mastername'),
        'mastersex'=>input('param.sex'),
        'masterphone'=>input('param.mobile'),
        'masterlesson'=>input('param.lesson'),
        'masterintro'=>input('param.intro'),
      ];
    }
 
   $res=Db::name("master")->insert($data);
    if($res){
      $this->redirect('master/index',5,'新增大师成功');
    }else{
      $this->error("新增大师失败");
    }
    return $this->fetch();
  }

public function edit(){
    $id=input('param.id');
    $master=Db::name('master')->find($id);
    $this->assign("master",$master);
    return $this->fetch();
}

public function update(){
   $file = request()->file('pic'); 
        //移动到框架应用根目录/public/static/images/ 目录下 
       var_dump($file);
        if($file){
          $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'imagemaster'); 
          if ($info) { 
              $filename=$info->getFilename(); 
          } else { 
              $this->error($file->getError()); 
          } 
        }
      
    if(request()->isPost()){
     
      $data=[
       'masterid'=>input('param.masterid'),
        'clubid'=>input('param.clubid'),

        'masterpic'=>$filename,
        'mastersex'=>input('param.mastersex'),
        'mastername'=>input('param.mastername'),
        'masterphone'=>input('param.mobile'),
        'masterlesson'=>input('param.lesson'),
        'masterintro'=>input('param.intro'),
      ];
    }
 
   $res=Db::name("master")->update($data);
    if($res){
      $this->redirect('master/index',5,'修改大师成功');
    }else{
      $this->error("修改大师失败");
    }
    return $this->fetch();
  }
}
